<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Kubernetes 巡检报告</title>

    <!-- Bootstrap 5 CSS -->
    <link
            href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"
            rel="stylesheet"
    />

    <!-- 自定义样式 -->
    <style>
        body {
            background-color: #f8f9fa;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
            color: #333;
        }

        /* 页面头部 */
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 3rem 0;
            margin-bottom: 2rem;
            text-align: center;
        }

        .header h1 {
            margin: 0;
            font-size: 2.2rem;
            font-weight: 600;
        }

        .header p {
            margin: 0.5rem 0 0 0;
            opacity: 0.9;
            font-size: 1.1rem;
        }

        /* 报告元信息：时间 + PDF按钮 */
        .report-meta {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
            padding: 1rem 0;
        }

        .download-pdf-btn {
            background-color: #007bff;
            border: none;
            color: white;
            padding: 0.6rem 1.2rem;
            border-radius: 6px;
            font-weight: 500;
            text-decoration: none;
            transition: all 0.2s ease;
        }

        .download-pdf-btn:hover {
            background-color: #0056b3;
            color: white;
            text-decoration: none;
        }

        /* 巡检卡片 */
        .inspection-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.07);
            margin-bottom: 2.5rem;
            overflow: hidden;
        }

        .inspection-card .card-header {
            background: linear-gradient(135deg, #495057 0%, #6c757d 100%);
            color: white;
            font-weight: 600;
            padding: 1rem 1.5rem;
            font-size: 1.1rem;
        }

        .inspection-card .card-body {
            padding: 1.5rem;
        }

        /* 表格样式优化 */
        .table-container {
            overflow-x: auto;
        }

        .table {
            margin-top: 1rem;
            width: 100%;
        }

        .table th {
            background-color: #f8f9fa;
            font-weight: 600;
            color: #495057;
            border-top: none;
            border-bottom: 2px solid #dee2e6;
        }

        .table td {
            vertical-align: middle;
            padding: 0.75rem;
        }

        .table tbody tr:hover {
            background-color: #f8f9fa;
        }

        .badge.bg-success {
            font-weight: 500;
        }

        .badge.bg-danger {
            font-weight: 500;
        }

        /* 域名标签容器 */
        .dns-container {
            display: flex;
            flex-wrap: wrap;
            gap: 5px;
            max-width: 300px;
        }

        /* 单个域名标签 */
        .dns-badge {
            background-color: #e9ecef;
            border-radius: 4px;
            padding: 3px 8px;
            font-size: 0.85em;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            max-width: 100%;
        }

        /* 状态徽章 */
        .badge {
            padding: 0.35em 0.65em;
            border-radius: 0.25rem;
            font-weight: 500;
        }

        /* 响应式表格标题 */
        @media (max-width: 768px) {
            .header h1 {
                font-size: 1.8rem;
            }
            .header p {
                font-size: 1rem;
            }
        }

        /* PDF 导出专用样式 */
        @media print {
            body {
                background-color: white;
                padding: 0;
                margin: 0;
            }

            .header {
                padding: 1.5rem 0;
                margin-bottom: 1rem;
            }

            .inspection-card {
                margin-bottom: 1.5rem;
                box-shadow: none;
                border: 1px solid #dee2e6;
            }

            .report-meta {
                margin-bottom: 1rem;
            }

            .download-pdf-btn {
                display: none;
            }

            .table th {
                background-color: #f1f3f5;
            }
        }
    </style>
</head>
<body>

<!-- 整个报告内容容器 -->
<div id="report-content">
    <!-- 页面头部 -->
    <div class="header text-center">
        <div class="container">
            <h1>🚀 XXX集群巡检报告</h1>
        </div>
    </div>
    <div class="container">

        <!-- 报告元信息：生成时间 + PDF下载按钮 -->
        <div class="report-meta">
            <div>
                <span class="badge bg-secondary">📅 生成时间: {{ .now }}</span>
            </div>
            <div>
                <button id="downloadPdfBtn" class="download-pdf-btn">📥 下载 PDF</button>
            </div>
        </div>


        <!-- ===================== -->
        <!-- 模块 1：Kubernetes 节点基础信息 -->
        <!-- ===================== -->
        <div class="inspection-card">
            <div class="card">
                <div class="card-header">
                    🔍 Kubernetes 节点基础信息
                </div>
                <div class="card-body">
                    {{ if .nodesInfo }}
                    <div class="table-container">
                        <table class="table table-striped table-hover">
                            <thead>
                            <tr>
                                <th>节点名称</th>
                                <th>状态</th>
                                <th>Kubelet 版本</th>
                                <th>架构</th>
                                <th>容器运行时</th>
                                <th>内核版本</th>
                                <th>节点地址</th>
                                <th>内存容量</th>
                                <th>CPU 核数</th>
                            </tr>
                            </thead>
                            <tbody>
                            {{ range .nodesInfo }}
                            <tr>
                                <td><strong>{{ .name }}</strong></td>
                                <td>
                                    {{ if .ready }}
                                    <span class="badge bg-success"> Ready</span>
                                    {{ else }}
                                    <span class="badge bg-danger"> NotReady</span>
                                    {{ end }}
                                </td>
                                <td>{{ .kubeletVersion }}</td>
                                <td>{{ .architecture }}</td>
                                <td>{{ .containerRuntime }}</td>
                                <td>{{ .kernelVersion }}</td>
                                <td>{{ .addresses }}</td>
                                <td>{{ .memory }}</td>
                                <td>{{ .CPU }}</td>
                            </tr>
                            {{ end }}
                            </tbody>
                        </table>
                    </div>
                    {{ else }}
                    <div class="alert alert-warning text-center" role="alert">
                        <h4>⚠️ 暂无节点数据</h4>
                        <p class="mb-0">未获取到任何 Kubernetes 节点信息，请检查集群连接状态。</p>
                    </div>
                    {{ end }}
                </div>
            </div>
        </div>

        <!-- ===================== -->
        <!-- 模块 2：Kubernetes 节点资源使用情况 -->
        <!-- ===================== -->
        <div class="inspection-card">
            <div class="card">
                <div class="card-header">
                    📊 Kubernetes 节点资源使用情况
                </div>
                <div class="card-body">
                    {{ if .nodeMetrics }}
                    <div class="table-container">
                        <table class="table table-striped table-hover">
                            <thead>
                            <tr>
                                <th>节点名称</th>
                                <th>CPU 使用量 (核)</th>
                                <th>内存使用量 (GB)</th>
                            </tr>
                            </thead>
                            <tbody>
                            {{ range .nodeMetrics }}
                            <tr>
                                <td><strong>{{ .Name }}</strong></td>
                                <td>{{ .CPUUsage }}</td>
                                <td>{{ .MemoryUsage }}</td>
                            </tr>
                            {{ end }}
                            </tbody>
                        </table>
                    </div>
                    {{ else }}
                    <div class="alert alert-warning text-center" role="alert">
                        <h4>⚠️ 暂无资源使用数据</h4>
                        <p class="mb-0">未获取到任何 Kubernetes 节点资源使用信息。</p>
                    </div>
                    {{ end }}
                </div>
            </div>
        </div>
        <!-- ===================== -->
        <!-- 模块 3：Kubernetes ingress证书过期巡检 -->
        <!-- ===================== -->
        <div class="inspection-card">
            <div class="card">
                <div class="card-header">
                    🔍 Kubernetes ingress证书过期巡检
                </div>
                <div class="card-body">
                    {{ if .tlsCertInfos }}
                    <div class="table-container">
                        <table class="table table-striped table-hover">
                            <thead>
                            <tr>
                                <th>证书名称</th>
                                <th>命名空间</th>
                                <th>证书类型</th>
                                <th>过期时间</th>
                                <th>域名</th>
                                <th>状态</th>
                            </tr>
                            </thead>
                            <tbody>
                            {{ range .tlsCertInfos }}
                            <tr>
                                <td><strong>{{ .SecretName }}</strong></td>
                                <td>{{ .Namespace }}</td>
                                <td>TLS Certificate</td>
                                <td>{{ .ExpireTime.Format "2006-01-02 15:04:05" }}</td>
                                <td>
                                    <div class="dns-container">
                                        <div class="dns-badge">{{ .CommonName }}</div>
                                        {{ range .DNSNames }}
                                        {{ if ne . $.CommonName }} <!-- 避免重复显示CommonName -->
                                        <div class="dns-badge">{{ . }}</div>
                                        {{ end }}
                                        {{ end }}
                                    </div>
                                </td>
                                <td>
                                    {{ if .IsExpired }}
                                    <span class="badge bg-danger">已过期</span>
                                    {{ else if lt .DaysRemaining 30 }}
                                    <span class="badge bg-warning text-dark">{{ .DaysRemaining }}天后过期</span>
                                    {{ else }}
                                    <span class="badge bg-success">有效</span>
                                    {{ end }}
                                </td>
                            </tr>
                            {{ end }}
                            </tbody>
                        </table>
                    </div>
                    {{ else }}
                    <div class="alert alert-info text-center" role="alert">
                        <h4>✅ 未发现 TLS 证书</h4>
                        <p class="mb-0">在集群中未发现任何类型为 kubernetes.io/tls 的 Secret。</p>
                    </div>
                    {{ end }}
                </div>
            </div>
        </div>

        <!-- ===================== -->
        <!-- 模块 4：Kubernetes 命名空间使用-->
        <!-- ===================== -->
        <div class="inspection-card">
            <div class="card">
                <div class="card-header">
                    📊 Kubernetes 命名空间使用情况
                </div>
                <div class="card-body">
                    {{ if .nsInfo }}
                    <div class="table-container">
                        <table class="table table-striped table-hover">
                            <thead>
                            <tr>
                                <th>命名空间名称</th>
                                <th>命名空间状态</th>
                                <th>pod数量</th>
                                <th>异常pod数量 </th>
                            </tr>
                            </thead>
                            <tbody>
                            {{ range .nsInfo }}
                            <tr>
                                <td><strong>{{ .name }}</strong></td>
                                <td>{{ .namespace_status}}</td>
                                <td>{{ .podNum }}</td>
                                <td>{{ .abnormal_pod_count }}</td>
                            </tr>
                            {{ end }}
                            </tbody>
                        </table>
                    </div>
                    {{ else }}
                    <div class="alert alert-warning text-center" role="alert">
                        <h4>⚠️ 暂无资源使用数据</h4>
                        <p class="mb-0">未获取到任何 Kubernetes 节点资源使用信息。</p>
                    </div>
                    {{ end }}
                </div>
            </div>
        </div>

        <!-- ===================== -->
        <!-- 模块 5：Kubernetes Pod 资源使用情况（CPU > 2C，内存 > 6G） -->
        <!-- ===================== -->
        <div class="inspection-card">
            <div class="card">
                <div class="card-header">
                    📊 Kubernetes Pod 资源使用情况（CPU > 2C，内存 > 6G）
                </div>
                <div class="card-body">
                    <!-- ===================== -->
                    <!-- 子模块 5.1：CPU 使用 > 2C 的 Pod -->
                    <!-- ===================== -->
                    <div>
                        <h5 class="text-primary mb-3">⚡ CPU 使用 > 2C 的 Pod</h5>
                        {{ if .highCPUPods }}
                        <div class="table-container">
                            <table class="table table-striped table-hover">
                                <thead>
                                <tr>
                                    <!-- 列宽与其他模块严格对齐（节点信息模块的列宽比例） -->
                                    <th style="width: 150px;">Pod 名称</th>
                                    <th style="width: 120px;">命名空间</th>
                                    <th style="width: 100px; text-align: right;">CPU 使用量 (核)</th>
                                </tr>
                                </thead>
                                <tbody>
                                {{ range .highCPUPods }}
                                <tr>
                                    <td><strong>{{ .PodName }}</strong></td>
                                    <td>{{ .Namespace }}</td>
                                    <td>{{ .CPUcores }}</td>
                                </tr>
                                {{ end }}
                                </tbody>
                            </table>
                        </div>
                        {{ else }}
                        <div class="alert alert-success text-center">
                            <h4>✅ CPU 使用正常</h4>
                            <p class="mb-0">未发现 CPU 使用超过 2C 的 Pod。</p>
                        </div>
                        {{ end }}
                    </div>

                    <!-- 分隔线（与其他模块完全一致） -->
                    <hr class="my-4">

                    <!-- ===================== -->
                    <!-- 子模块 5.2：内存使用 > 6G 的 Pod -->
                    <!-- ===================== -->
                    <div>
                        <h5 class="text-success mb-3">💧 内存使用 > 6G 的 Pod</h5>
                        {{ if .highMemoryPods }}
                        <div class="table-container">
                            <table class="table table-striped table-hover">
                                <thead>
                                <tr>
                                    <th style="width: 150px;">Pod 名称</th>
                                    <th style="width: 120px;">命名空间</th>
                                    <th style="width: 100px; text-align: right;">内存使用量 (GB)</th>
                                </tr>
                                </thead>
                                <tbody>
                                {{ range .highMemoryPods }}
                                <tr>
                                    <td><strong>{{ .PodName }}</strong></td>
                                    <td>{{ .Namespace }}</td>
                                    <td>{{ .MemoryGB }}</td>
                                </tr>
                                {{ end }}
                                </tbody>
                            </table>
                        </div>
                        {{ else }}
                        <div class="alert alert-success text-center">
                            <h4>✅ 内存使用正常</h4>
                            <p class="mb-0">未发现 内存 使用超过 6G 的 Pod。</p>
                        </div>
                        {{ end }}
                    </div>
                </div>
            </div>
        </div>

        <!-- ===================== -->
        <!-- 模块 6：Kubernetes pod频繁重启-->
        <!-- ===================== -->
        <div class="inspection-card">
            <div class="card">
                <div class="card-header">
                    📊 Kubernetes pod频繁重启检查
                </div>
                <div class="card-body">
                    {{ if .frequentRestartPods }}
                    <div class="table-container">
                        <table class="table table-striped table-hover">
                            <thead>
                            <tr>
                                <th>pod名称</th>
                                <th>命名空间</th>
                                <th>重启次数</th>
                                <th>最后一次重启时间 </th>
                            </tr>
                            </thead>
                            <tbody>
                            {{ range .frequentRestartPods }}
                            <tr>
                                <td><strong>{{ .PodName }}</strong></td>
                                <td>{{ .Namespace}}</td>
                                <td>{{ .RestartCount }}</td>
                                <td>{{ .LastRestartTime }}</td>
                            </tr>
                            {{ end }}
                            </tbody>
                        </table>
                    </div>
                    {{ else }}
                    <div class="alert alert-warning text-center" role="alert">
                        <h4>⚠️ 暂无资源使用数据</h4>
                        <p class="mb-0">未获取到任何 Kubernetes 节点资源使用信息。</p>
                    </div>
                    {{ end }}
                </div>
            </div>
        </div>

    </div>
</div> <!-- 结束 report-content -->

<!-- ===================== -->
<!-- PDF 生成逻辑（放在 body 底部） -->
<!-- ===================== -->
<script>
    document.addEventListener('DOMContentLoaded', function() {
        document.getElementById('downloadPdfBtn').addEventListener('click', function() {
            // 1. 隐藏按钮
            this.style.display = 'none';

            // 2. 获取整个报告内容容器
            const reportContent = document.getElementById('report-content');

            // 3. 添加临时类名以优化PDF样式
            reportContent.classList.add('pdf-exporting');

            // 4. 配置 html2pdf 参数
            const opt = {
                margin: 15, // PDF 边距（mm）
                filename: `Kubernetes巡检报告_${new Date().toLocaleDateString().replace(/\//g, '-')}.pdf`,
                image: { type: 'jpeg', quality: 0.95 },
                html2canvas: {
                    scale: 2,
                    useCORS: true,
                    logging: false,
                    windowWidth: reportContent.scrollWidth,
                    backgroundColor: '#FFFFFF'
                },
                jsPDF: {
                    unit: 'mm',
                    format: 'a4',
                    orientation: 'portrait'
                },
                pagebreak: {
                    mode: ['avoid-all', 'css', 'legacy']
                }
            };

            // 5. 执行 PDF 生成与下载
            html2pdf()
                .set(opt)
                .from(reportContent)
                .save()
                .then(() => {
                    // 恢复按钮显示
                    this.style.display = 'inline-block';
                    reportContent.classList.remove('pdf-exporting');
                })
                .catch(err => {
                    console.error('PDF 生成失败:', err);
                    this.style.display = 'inline-block';
                    reportContent.classList.remove('pdf-exporting');
                    alert('PDF 生成失败，请重试！错误：' + err.message);
                });
        });
    });
</script>

<!-- Bootstrap 5 JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/html2pdf.js@0.10.1/dist/html2pdf.bundle.min.js"></script>
</body>
</html>